package edu.neusoft.kongfanfei317.yuejian1.activity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import edu.neusoft.kongfanfei317.yuejian1.R;
import edu.neusoft.kongfanfei317.yuejian1.bean.RegisterBean;
import edu.neusoft.kongfanfei317.yuejian1.listener.TListener;
import edu.neusoft.kongfanfei317.yuejian1.model.RegisterModel;

public class RegisterActivity extends BaseActivity {
    EditText reg_name;
    EditText reg_pass;
    EditText reg_pasword;
    EditText reg_address;
    EditText reg_number;
    EditText reg_comment;
    Button btn_reg, btn_back;
    TListener<RegisterBean> TListener = new TListener<RegisterBean>() {
        @Override
        public void onResponse(RegisterBean registerBean) {
            if (registerBean.getSuccess().equals("0")) {
                showToast("注册失败！");

            } else {
                showToast("注册成功！");
                returnMsg();
            }

        }

        @Override
        public void onFail(String msg) {
            showToast(msg);

        }
    };

    @Override
    void initView() {
        setLayout_file(R.layout.activity_register);
        reg_name = (EditText)findViewById(R.id.reg_name);
        reg_pass = (EditText)findViewById(R.id.reg_pass);
        reg_pasword = (EditText)findViewById(R.id.reg_pasword);
        reg_address = (EditText)findViewById(R.id.reg_address);
        reg_number = (EditText)findViewById(R.id.reg_number);
        reg_comment = (EditText)findViewById(R.id.reg_comment);
        btn_reg = (Button)findViewById(R.id.btn_reg);
        btn_back = (Button)findViewById(R.id.btn_back);
    }

    @Override
    void initEvent() {
        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(reg_name.getText().toString().trim().equals("")||reg_pass.getText().toString().trim().equals("")
                        ||reg_number.getText().toString().trim().equals("")||reg_address.getText().toString().trim().equals("")
                        ||reg_comment.getText().toString().trim().equals("")){
                    showToast("每一项都要输入！");
                }
                else if (!(reg_pass.getText().toString().equals(reg_pasword.getText().toString()))) {
                    showToast("密码不一致!");
                }
                else{
                    RegisterModel registerModel = new RegisterModel();
                    registerModel.getRegister(reg_name.getText().toString(),reg_pass.getText().toString()
                            ,reg_number.getText().toString(),reg_address.getText().toString(),reg_comment.getText().toString(),TListener);

                }
            }
        });
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(RegisterActivity.this,LoginActivity.class);
                startActivityForResult(intent,1);
            }
        });
    }

    @Override
    void initData() {

    }
    void returnMsg(){
        Intent intent = new Intent();
        intent.putExtra("username",reg_name.getText().toString());
        intent.putExtra("userpass",reg_pass.getText().toString());
        intent.setClass(RegisterActivity.this,LoginActivity.class);
        startActivity(intent);
        finish();
    }



}
